package scrape;
import java.math.BigDecimal;
import java.net.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.*;

import javax.imageio.ImageIO;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.net.ssl.HttpsURLConnection;


public class URLReader {
	
	static ArrayList ids;
	
	public static void main(String[] args) throws Exception {

		ids = new ArrayList();
		boolean flag = true;
		int i = 1;
		while(flag){
			
			flag = parseBCNLowCost(i);
			i++;
		}
		
		System.out.println("--------------------------------");
		
		for (Iterator iterator = ids.iterator(); iterator.hasNext();) {
			String id = (String) iterator.next();
			if (parseSubasta(id)){
				System.out.println(id);
			}

		}
		
		/*
		463757
		504426
		513959
		520751
		517546
		518628
		520571*/
		 
		

	}
	
	public static boolean parseSubasta(String id){

		String url = "https://www.administraciondejusticia.gob.es/paj/publico/ciudadano/servicios/subastas_judiciales/busqueda_subastas/detalle_subasta/?itemId=" + id;

		boolean flag = false;
		BufferedReader br = null;
		try {
		    // Create a trust manager that does not validate certificate chains
		    final TrustManager[] trustAllCerts = new TrustManager[] { 
		    	new X509TrustManager() {
		        @Override
		        public X509Certificate[] getAcceptedIssuers() {
		            return null;
		        }
				@Override
				public void checkClientTrusted(X509Certificate[] arg0,
						String arg1) throws CertificateException {
					// TODO Auto-generated method stub
					
				}
				@Override
				public void checkServerTrusted(X509Certificate[] arg0,
						String arg1) throws CertificateException {
					// TODO Auto-generated method stub
					
				}
		    } };
		    
		    // Install the all-trusting trust manager
		    final SSLContext sslContext = SSLContext.getInstance( "SSL" );
		    sslContext.init( null, trustAllCerts, new java.security.SecureRandom() );
		    // Create an ssl socket factory with our all-trusting manager
		    final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
		    
		    
		    // All set up, we can get a resource through https now:
		    final URLConnection urlCon = new URL( url ).openConnection();
		    // Tell the url connection object to use our socket factory which bypasses security checks
		    ( (HttpsURLConnection) urlCon ).setSSLSocketFactory( sslSocketFactory );
		    
		    final InputStream input = urlCon.getInputStream();
		    br = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
	
		    String inputLine;
			int contador = 0;
			while ((inputLine = br.readLine()) != null && contador<10) {
				//System.out.println(inputLine); 
				if (inputLine.contains("vivienda")) {
					if (br != null)
						try {
							br.close();
						} catch (Exception fe) {
						}
					return true;
				}
				
			}	
		    input.close();
		    
		    
		    
		} catch ( final Exception e ) {
		    e.printStackTrace();
		}
		
		if (br != null)
			try {
				br.close();
			} catch (Exception fe) {
			}
		return false;
	}
	
	
	public static boolean parseBCNLowCost(int page){

		//System.setProperty("javax.net.ssl.trustStore","C:/Archivos de programa/Java/jdk1.6.0_18/jre/lib/security/cacerts"); 
		//System.setProperty( "sun.security.ssl.allowUnsafeRenegotiation", "true" );
		
		String url = "https://www.administraciondejusticia.gob.es/paj/publico/ciudadano/servicios/subastas_judiciales/busqueda_subastas/?palabraBuscar=&tipoBienes=&fechaInicio=14%2F04%2F2014&fechaCierre=&tasacionDesde=&tasacionHasta=30000&provinciaSubasta=Barcelona&municipioSubasta=&localizacionSubasta=&busquedaPrimera=false&paginaDestino=" + Integer.toString(page);
		//https://www.administraciondejusticia.gob.es/paj/publico/ciudadano/servicios/subastas_judiciales/busqueda_subastas/detalle_subasta/?itemId=437086
		boolean flag = false;
		BufferedReader br = null;
		try {
		    // Create a trust manager that does not validate certificate chains
		    final TrustManager[] trustAllCerts = new TrustManager[] { 
		    	new X509TrustManager() {
		        @Override
		        public X509Certificate[] getAcceptedIssuers() {
		            return null;
		        }
				@Override
				public void checkClientTrusted(X509Certificate[] arg0,
						String arg1) throws CertificateException {
					// TODO Auto-generated method stub
					
				}
				@Override
				public void checkServerTrusted(X509Certificate[] arg0,
						String arg1) throws CertificateException {
					// TODO Auto-generated method stub
					
				}
		    } };
		    
		    // Install the all-trusting trust manager
		    final SSLContext sslContext = SSLContext.getInstance( "SSL" );
		    sslContext.init( null, trustAllCerts, new java.security.SecureRandom() );
		    // Create an ssl socket factory with our all-trusting manager
		    final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
		    
		    
		    // All set up, we can get a resource through https now:
		    final URLConnection urlCon = new URL( url ).openConnection();
		    // Tell the url connection object to use our socket factory which bypasses security checks
		    ( (HttpsURLConnection) urlCon ).setSSLSocketFactory( sslSocketFactory );
		    
		    final InputStream input = urlCon.getInputStream();
		    br = new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
	
		    String inputLine;
			int contador = 0;
			while ((inputLine = br.readLine()) != null && contador<10) {
				//System.out.println(inputLine); 
				if (inputLine.contains("/paj/publico/ciudadano/servicios/subastas_judiciales/busqueda_subastas/detalle_subasta")) {
					contador++;
					String[] link = inputLine.split("itemId[=\\d\\d\\d\\d\\d\\d]");
					for (int i = 1; i < link.length; i++) {

						String linkFinal = link[i].substring(0, 6);
						ids.add(linkFinal);
						System.out.println(linkFinal);
						flag = true;
					}
				}
				
			}	
		    input.close();
		    
		    
		    
		} catch ( final Exception e ) {
		    e.printStackTrace();
		}
		
		if (br != null)
			try {
				br.close();
			} catch (Exception fe) {
			}
		return flag;
	}

	
	

}
